package com.example.fill;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

public class CustomCircleView extends View {

    private Paint paint = new Paint();
    private Path path = new Path();
    private float centerX;
    private float centerY;
    private float radius;

    public CustomCircleView(Context context) {
        super(context);
        init();
    }

    public CustomCircleView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        paint.setAntiAlias(true);
        paint.setStyle(Paint.Style.FILL);
        paint.setColor(0xFF000000); // Set the color of the circle
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        path.reset();
        path.addCircle(centerX, centerY, radius, Path.Direction.CW); // Add the circle to the path
        canvas.drawPath(path, paint); // Draw the path (circle) on the canvas
    }

    public void setCenter(float centerX, float centerY) {
        this.centerX = centerX;
        this.centerY = centerY;
        invalidate(); // Invalidate the view to trigger a redraw
    }

    public void setRadius(float radius) {
        this.radius = radius;
        invalidate(); // Invalidate the view to trigger a redraw
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                float touchX = event.getX();
                float touchY = event.getY();
                if (isPointInCircle(touchX, touchY, centerX, centerY, radius)) {
                    // The touch is inside the circle, do something here (e.g., record the position)
                    return true; // Indicate that the event was handled (i.e., prevent further handling)
                } else {
                    // Calculate the intersection point of the circle and the line from the touch point to the circle's center
                    float[] intersectionPoint = calculateIntersectionPoint(touchX, touchY, centerX, centerY, radius);
                    if (intersectionPoint != null) {
                        // The touch is outside the circle, do something with the intersection point here (e.g., record it)
                        return true; // Indicate that the event was handled (i.e., prevent further handling)
                    } else {
                        return false; // The touch is outside the circle and there is no intersection point, let further handling occur (if any)
                    }
                }
            default:
                return super.onTouchEvent(event); // Let other events be handled by the super class (if any)
        }
    }

    private boolean isPointInCircle(float x, float y, float centerX, float centerY, float radius) {
        double distance = Math.sqrt(Math.pow(x - centerX, 2) + Math.pow(y - centerY, 2));
        return distance <= radius; // Check if the distance is less than or equal to the radius of the circle
    }

    private float[] calculateIntersectionPoint(float touchX, float touchY, float centerX, float centerY, float radius) {
        float[] intersectionPoint = new float[2]; // Array to store the intersection point coordinates (x and y)
        double distanceFromCenter = Math.sqrt(Math.pow(touchX - centerX, 2) + Math.pow(touchY - centerY, 2)); // Calculate the distance from the touch point to the circle's center
        if (distanceFromCenter > radius) { // If the touch point is outside the circle...
            double angleFromTouchToCenter = Math.atan2(touchY - centerY, touchX - centerX); // Calculate the angle from the touch point to the circle's center in radians
            {
                double distanceFromCircle = radius - distanceFromCenter; // Calculate the distance from the circle to the touch point
                double angleFromCircleToIntersection = Math.acos(distanceFromCircle / radius); // Calculate the angle from the circle to the intersection point in radians (0 ≤ angle ≤ π)
                float intersectionX = centerX + (float) (radius * Math.cos(angleFromCircleToIntersection)); // Calculate the x coordinate of the intersection point
                float intersectionY = centerY + (float) (radius * Math.sin(angleFromCircleToIntersection)); // Calculate the y coordinate of the intersection point
                intersectionPoint[0] = intersectionX; // Store the x coordinate in the intersectionPoint array
                intersectionPoint[1] = intersectionY; // Store the y coordinate in the intersectionPoint array

            }
        }
        return intersectionPoint; // Return the intersection point coordinates
    }
}
